/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 6/6/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;        
namespace DataAccessLayer{
      /// <summary>
    /// Define ChiTietPhieuCapNhatTonKhoDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in ChiTietPhieuCapNhatTonKhoDao class , not here.
    /// </summary>
    public class ChiTietPhieuCapNhatTonKhoDaoBase {
    private static ILog logger = null;
            #region Constructor
             /// <summary>
            /// Define Constructor.
             /// </summary>
            public ChiTietPhieuCapNhatTonKhoDaoBase(){}
             static ChiTietPhieuCapNhatTonKhoDaoBase()
            {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(ChiTietPhieuCapNhatTonKhoDaoBase).FullName);
            }
 #endregion
              /// <summary>
             /// Read data from IDataReader, convert to ChiTietPhieuCapNhatTonKho Object.
            /// </summary>
            private static ChiTietPhieuCapNhatTonKho GetChiTietPhieuCapNhatTonKhoByDataReader(IDataReader reader)
    {
      String methodName = "GetChiTietPhieuCapNhatTonKhoByDataReader";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
        try
      {
        ChiTietPhieuCapNhatTonKho result = new ChiTietPhieuCapNhatTonKho();
              //read MaPhieuCapNhatTonKho field data.
             
            result.MaPhieuCapNhatTonKho=ManagerDao.GetintDataFromDataReader(reader,"MaPhieuCapNhatTonKho");
            
              //read MaMatHang field data.
             
            result.MaMatHang=ManagerDao.GetstringDataFromDataReader(reader,"MaMatHang");
            
              //read MaDonViTinh field data.
             
            result.MaDonViTinh=ManagerDao.GetintDataFromDataReader(reader,"MaDonViTinh");
            
              //read SoLuongCu field data.
             
            result.SoLuongCu=ManagerDao.GetintDataFromDataReader(reader,"SoLuongCu");
            
              //read SoLuongMoi field data.
             
            result.SoLuongMoi=ManagerDao.GetintDataFromDataReader(reader,"SoLuongMoi");
            
             return result;
      }
      catch (Exception e)
      {
        // write error to log file.
        logger.Error(methodName, e);
        return null;
      }
    }
              /// <summary>
        /// Define function Insert for ChiTietPhieuCapNhatTonKho Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Insert(ChiTietPhieuCapNhatTonKho obj)
            {
                bool result = false;
              String methodName = "InsertChiTietPhieuCapNhatTonKho";
              if (logger.IsDebugEnabled)
              {
                LoggingUtility.logMethodEntry(logger, methodName);
              }
              DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertChiTietPhieuCapNhatTonKho");            
            
                 //Add value "MaPhieuCapNhatTonKho"  paramater.
				ManagerDao.AddInParameter(cmd, "MaPhieuCapNhatTonKho", DbType.Int32,obj.MaPhieuCapNhatTonKho);
                 //Add value "MaMatHang"  paramater.
				ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString,
					ConvertValueObject.CastObjValueToInsertDB(obj.MaMatHang));
                 //Add value "MaDonViTinh"  paramater.
				ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32,
					ConvertValueObject.CastObjValueToInsertDB(obj.MaDonViTinh));
                 //Add value "SoLuongCu"  paramater.
				ManagerDao.AddInParameter(cmd, "SoLuongCu", DbType.Int32,
					ConvertValueObject.CastObjValueToInsertDB(obj.SoLuongCu));
                 //Add value "SoLuongMoi"  paramater.
				ManagerDao.AddInParameter(cmd, "SoLuongMoi", DbType.Int32,
					ConvertValueObject.CastObjValueToInsertDB(obj.SoLuongMoi));    
            //add paramater store return value.        
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
              {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id>0)//Successfull.
                {
                  result = true;
                }
                else//Error.
                {
                  result = false;
                }
              }
              catch (Exception e)
              {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
              }
              return result;
            }
             
               /// <summary>
        /// Define function Update for ChiTietPhieuCapNhatTonKho Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Update(ChiTietPhieuCapNhatTonKho obj)
          {
          bool result = false;
          String methodName = "UpdateChiTietPhieuCapNhatTonKho";
          if (logger.IsDebugEnabled)
          {
            LoggingUtility.logMethodEntry(logger, methodName);
          }
             DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateChiTietPhieuCapNhatTonKho");
           

                 //Add value "MaPhieuCapNhatTonKho"  paramater.
				ManagerDao.AddInParameter(cmd, "MaPhieuCapNhatTonKho", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.MaPhieuCapNhatTonKho));
                 //Add value "MaMatHang"  paramater.
				ManagerDao.AddInParameter(cmd, "MaMatHang", DbType.AnsiString,ConvertValueObject.CastObjValueToInsertDB(obj.MaMatHang));
                 //Add value "MaDonViTinh"  paramater.
				ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.MaDonViTinh));
                 //Add value "SoLuongCu"  paramater.
				ManagerDao.AddInParameter(cmd, "SoLuongCu", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.SoLuongCu));
                 //Add value "SoLuongMoi"  paramater.
				ManagerDao.AddInParameter(cmd, "SoLuongMoi", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.SoLuongMoi));           
            // add parameter store return value.
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
           try
          {
            ManagerDao.ExecuteNonQuery(cmd);
            int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
            if (rows == 1)//Successfull.
            {
              result = true;
            }
            else//Error.
            {
              result = false;
            }

          }
          catch (Exception e)
          {
            //write error Message to log file.
            logger.Error(methodName, e);
            result = false;
          }
          return result;
                }
/// <summary>
/// Get Data ChiTietPhieuCapNhatTonKho object. 
/// </summary>
/// <param name="maPhieuCapNhatTonKho" >This is primary key to get data.</param>
/// <returns>ChiTietPhieuCapNhatTonKho object.</returns>
              
           public static ChiTietPhieuCapNhatTonKho Get(int maPhieuCapNhatTonKho)
    {
      String methodName = "GetChiTietPhieuCapNhatTonKho";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
      try
      {
        ChiTietPhieuCapNhatTonKho result = null;
        DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetChiTietPhieuCapNhatTonKho");
                 //Add value "MaPhieuCapNhatTonKho"  paramater.
				ManagerDao.AddInParameter(dbCommnand, "MaPhieuCapNhatTonKho", DbType.Int32,maPhieuCapNhatTonKho);
        IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
        if (reader.Read())
        {
          result= GetChiTietPhieuCapNhatTonKhoByDataReader(reader);

        }
        reader.Close();
        return result;
      }
      catch (System.Exception e)
      {
        //write error message to log file.
        logger.Error("Get", e);
        return null;
      }
    }
              /// <summary>
        /// Get All ChiTietPhieuCapNhatTonKho object from table ChiTietPhieuCapNhatTonKho.
        /// </summary>
        /// <returns>List ChiTietPhieuCapNhatTonKho object.</returns>
             public static List<ChiTietPhieuCapNhatTonKhoResult> GetAll()
                {
                 String methodName = "GetAll";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<ChiTietPhieuCapNhatTonKhoResult> listChiTietPhieuCapNhatTonKho = new List<ChiTietPhieuCapNhatTonKhoResult>();
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllChiTietPhieuCapNhatTonKho");

        using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
        {
          while (reader.Read())
          {
            listChiTietPhieuCapNhatTonKho.Add(GetChiTietPhieuCapNhatTonKhoResultByDataReader(reader));
          }
          reader.Close();
        }

        return listChiTietPhieuCapNhatTonKho;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }

             /// <summary>
        /// Search ChiTietPhieuCapNhatTonKho object in table ChiTietPhieuCapNhatTonKho with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List ChiTietPhieuCapNhatTonKhoResult object.</returns>
             public static List<ChiTietPhieuCapNhatTonKhoResult> SearchChiTietPhieuCapNhatTonKhoSomeCondition(ChiTietPhieuCapNhatTonKho t)
                {
                 String methodName = "SearchChiTietPhieuCapNhatTonKhoSomeCondition";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<ChiTietPhieuCapNhatTonKhoResult> listChiTietPhieuCapNhatTonKho = new List<ChiTietPhieuCapNhatTonKhoResult>();
        DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchChiTietPhieuCapNhatTonKhoByCondition");
              //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String,ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                 //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32,  t.ObjectPaging.PageSize);
         //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String,  t.ObjectSortOrder.SortExpression);
          using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
        {
          while (reader.Read())
          {
            listChiTietPhieuCapNhatTonKho.Add(GetChiTietPhieuCapNhatTonKhoResultByDataReader(reader));
            if(t.ToltalRow<=0)
                 t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
          }
         
          reader.Close();
        }

        return listChiTietPhieuCapNhatTonKho;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }
/// <summary>
/// Delete ChiTietPhieuCapNhatTonKho object. 
/// </summary>
/// <param name="MaPhieuCapNhatTonKho" >This is primary key to delete.</param>
/// <returns>True/False.</returns>

            public static bool Delete(int MaPhieuCapNhatTonKho)
    {
      String methodName = "DeleteChiTietPhieuCapNhatTonKho";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteChiTietPhieuCapNhatTonKho");
                //Add value "MaPhieuCapNhatTonKho"  paramater.
				ManagerDao.AddInParameter(dbCommand, "MaPhieuCapNhatTonKho", DbType.Int32,MaPhieuCapNhatTonKho);        
         //Add value returnValue  paramater.
        ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
        try
        {
          ManagerDao.ExecuteNonQuery(dbCommand);        
          int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
          if (parameterValue == 1)
          {
            return true;
          }
        }
        catch (Exception)
        {
          throw;
        }
        return false;
      }
      catch (Exception e)
      {
        //write error message to log file.
        logger.Error(methodName, e);
        return false;
      }
    }
            /// <summary>
             /// Read data from IDataReader, convert to ChiTietPhieuCapNhatTonKhoResult Object.
            /// </summary>
            private static ChiTietPhieuCapNhatTonKhoResult GetChiTietPhieuCapNhatTonKhoResultByDataReaderByLanguage(IDataReader reader,string LanguageId)
    {
      String methodName = "GetChiTietPhieuCapNhatTonKhoResultByDataReader";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
        try
      {
        ChiTietPhieuCapNhatTonKhoResult result = new ChiTietPhieuCapNhatTonKhoResult();
            //read MaPhieuCapNhatTonKho field data.
             result.MaPhieuCapNhatTonKho =ManagerDao.GetintDataFromDataReader(reader,"MaPhieuCapNhatTonKho");
            
            //read MaMatHang field data.
             result.MaMatHang =ManagerDao.GetstringDataFromDataReader(reader,"MaMatHang");
            
            //read MaDonViTinh field data.
             result.MaDonViTinh =ManagerDao.GetintDataFromDataReader(reader,"MaDonViTinh");
            
            //read SoLuongCu field data.
             result.SoLuongCu =ManagerDao.GetintDataFromDataReader(reader,"SoLuongCu");
            
            //read SoLuongMoi field data.
             result.SoLuongMoi =ManagerDao.GetintDataFromDataReader(reader,"SoLuongMoi");
            
             return result;
      }
      catch (Exception e)
      {
        // write error to log file.
        logger.Error(methodName, e);
        return null;
      }
    }
            /// <summary>
             /// Read data from IDataReader, convert to ChiTietPhieuCapNhatTonKhoResult Object.
            /// </summary>
            private static ChiTietPhieuCapNhatTonKhoResult GetChiTietPhieuCapNhatTonKhoResultByDataReader(IDataReader reader)
    {
      String methodName = "GetChiTietPhieuCapNhatTonKhoResultByDataReader";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
        try
      {
        ChiTietPhieuCapNhatTonKhoResult result = new ChiTietPhieuCapNhatTonKhoResult();
            //read MaPhieuCapNhatTonKho field data.
             result.MaPhieuCapNhatTonKho =ManagerDao.GetintDataFromDataReader(reader,"MaPhieuCapNhatTonKho");
            
            //read MaMatHang field data.
             result.MaMatHang =ManagerDao.GetstringDataFromDataReader(reader,"MaMatHang");
            
            //read MaDonViTinh field data.
             result.MaDonViTinh =ManagerDao.GetintDataFromDataReader(reader,"MaDonViTinh");
            
            //read SoLuongCu field data.
             result.SoLuongCu =ManagerDao.GetintDataFromDataReader(reader,"SoLuongCu");
            
            //read SoLuongMoi field data.
             result.SoLuongMoi =ManagerDao.GetintDataFromDataReader(reader,"SoLuongMoi");
            
             return result;
      }
      catch (Exception e)
      {
        // write error to log file.
        logger.Error(methodName, e);
        return null;
      }
    }  
           
	                }
                }